package com.zy.ivideo.controller;

import com.zy.ivideo.entity.ResponseEntity;
import com.zy.ivideo.entity.UserEntity;
import com.zy.ivideo.service.UserService;
import com.zy.ivideo.token.PassToken;
import com.zy.ivideo.token.UserToken;
import com.zy.ivideo.utils.ResponseUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(value = "用户模块API接口", description = "该模块提供用户模块服务", tags = "用户模块")
public class UserController {

    @Autowired
    UserService userService;

    @UserToken
    @ApiOperation(value = "获取所有用户", notes = "获取所有用户方法", httpMethod = "GET")
    @GetMapping("/getUsers")
    public ResponseEntity<List<UserEntity>> getUsers(){
        return userService.getUsers();
    }

    @PassToken
    @ApiOperation(value = "用户登录方法", notes = "用户登录方法", httpMethod = "POST")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(dataType = "String", name = "username", value = "用户id", required = true),
            @ApiImplicitParam(dataType = "String", name = "password", value = "密码", required = true)
    })
    @PostMapping("/login")
    public ResponseEntity<UserEntity> login(String username,String password){
        return userService.login(username,password);
    }

    @PassToken
    @ApiOperation(value = "用户注册方法", notes = "用户注册方法", httpMethod = "POST")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(dataType = "String", name = "username", value = "用户名称", required = true),
            @ApiImplicitParam(dataType = "String", name = "password", value = "密码", required = true)
    })
    @PostMapping("/register")
    public ResponseEntity<UserEntity> register(String username,String password){
        return userService.register(username,password);
    }

    /**
     * 修改密码
     * @param newPwd
     * @param userid
     * @return
     */
    @PassToken
    @ApiOperation(value = "修改密码", notes = "修改密码", httpMethod = "PUT")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(dataType = "String", name = "newPwd", value = "用户新密码", required = true),
            @ApiImplicitParam(dataType = "int", name = "userid", value = "用户id", required = true)
    })
    @PutMapping("/modifyPwd")
    public ResponseEntity<Boolean> modifyPwd(String newPwd,int userid){
        return userService.modifyPwd(newPwd,userid);
    }
}
